using UnityEditor;
using UnityEngine;

public class InputDialog : EditorWindow
{
    private string userInput = "";
    private System.Action<string> callback;

    public static void ShowWindow(string text, System.Action<string> callback)
    {
        var dialog = GetWindow<InputDialog>();
        dialog.userInput = text;
        dialog.callback = callback;
    }

    public static string Show(string title, string message, string defaultInput)
    {
        InputDialog window = ScriptableObject.CreateInstance<InputDialog>();
        window.titleContent = new GUIContent(title);
        window.userInput = defaultInput;
        window.ShowModal();
        return window.userInput;
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("Message", EditorStyles.wordWrappedLabel);
        userInput = EditorGUILayout.TextField("Input", userInput);


        GUILayout.BeginHorizontal();
        if (GUILayout.Button("OK"))
        {
            callback?.Invoke(userInput);
            this.Close();
        }
        if (GUILayout.Button("Cancel"))
        {
            userInput = null;
            this.Close();
        }
        GUILayout.EndHorizontal();
    }
}